package com.yasha.ulti;

import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.http.impl.execchain.RetryExec;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Patterns;

public class Tools {

	public static int TYPE_WIFI = 1;
	public static int TYPE_MOBILE = 2;
	public static int TYPE_NOT_CONNECTED = 0;

	public static int getConnectivityStatus(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (null != activeNetwork) {
			if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
				return TYPE_WIFI;
			if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
				return TYPE_MOBILE;
		}
		return TYPE_NOT_CONNECTED;
	}

	public static String getConnectivityStatusString(Context context) {
		int conn = getConnectivityStatus(context);
		String status = null;
		if (conn == TYPE_WIFI) {
			status = "Wifi enabled";
		} else if (conn == TYPE_MOBILE) {
			status = "Mobile data enabled";
		} else if (conn == TYPE_NOT_CONNECTED) {
			status = "Not connected to Internet";
		}
		return status;
	}

	public static String getUserEmail(Context context) {
		String email = "";
		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
		Account[] accounts = AccountManager.get(context).getAccounts();
		for (Account account : accounts) {
			if (emailPattern.matcher(account.name).matches()) {
				email = account.name;
			}
		}

		return email;
	}

	private static boolean debug = true;

	public static void Log(String title, String message) {
		if (debug == true)
			android.util.Log.i(title, message);
	}

	public static String sharePreferencesUserKey = "com.yasha.data.user";
	public static String UserEmailkey = "email";
	public static String UserPhonekey = "phone";
	public static String UserIDkey = "id";

	public static String loadSavedPreferences(Context context, String code,
			String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				code, Context.MODE_PRIVATE);
		String value = sharedPreferences.getString(key, "");
		return value;
	}

	public static boolean savePreferences(Context context, String code,
			String key, String value) {
		boolean result = true;
		try {
			SharedPreferences sharedPreferences = context.getSharedPreferences(
					code, Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.putString(key, value);
			editor.commit();
		} catch (Exception e) {
			result = false;
		}
		return result;

	}

	public static void saveLocale(String lang , Context cxt) {
		String langPref = "Language";
		SharedPreferences prefs = cxt.getSharedPreferences("CommonPrefs",
				Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(langPref, lang);
		editor.commit();
	}

	public static void loadLocale(Context cxt) {
		String langPref = "Language";
		SharedPreferences prefs = cxt.getSharedPreferences("CommonPrefs",
				Activity.MODE_PRIVATE);
		String language = prefs.getString(langPref, "vi_VN");
		changeLang(language,cxt);
	}

	public static String getLocaleString(Context cxt) {
		String langPref = "Language";
		SharedPreferences prefs = cxt.getSharedPreferences("CommonPrefs",
				Activity.MODE_PRIVATE);
		String language = prefs.getString(langPref, "");
		return language;
	}
	//en_US => en 
	//vi_VN => vi
	
	private static Locale myLocale;
	
	public static void changeLang(String lang, Context cxt) {
		if (lang.equalsIgnoreCase(""))
			return;
		myLocale = new Locale(lang);
		saveLocale(lang,cxt);
		Locale.setDefault(myLocale);
		android.content.res.Configuration config = new android.content.res.Configuration();
		config.locale = myLocale;
		cxt.getResources().updateConfiguration(config,
				cxt.getResources().getDisplayMetrics());
		
	}
}
